/**
 * Copyright (C), 2020-2021
 * FileName: LoginUserApiImpl
 * Author:   86189
 * Date:     2021/3/8 20:02
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.itheima.user.api;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.user.mapper.AccountMapper;
import com.itheima.user.mapper.LoginUserMapper;
import com.itheima.user.pojo.Account;
import com.itheima.user.pojo.User;
import com.sun.corba.se.spi.ior.ObjectId;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.UUID;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author 86189
 * @create 2021/3/8
 * @since 1.0.0
 */
@Service(version = "1.0.0")
public class LoginUserApiImpl implements LoginUserApi{

    @Autowired
    private LoginUserMapper loginUserMapper;

    @Autowired
    private AccountMapper accountMapper;

    /**
     * 添加用户
     * @param user
     * @return
     */
    @Override
    public Long loginUser(User user) {

//        String userId = UUID.randomUUID().toString();
//        String userId = "1";
//        user.setId(Long.valueOf(userId));
//        String accountId = UUID.randomUUID().toString();
//        String accountId = "11";
//        user.setAccountId(Long.valueOf(accountId));
        user.setCreateTime(new Date());
        user.setAccountId(user.getId());

        int insert = loginUserMapper.insert(user);

        Account account = new Account();
        account.setAccountName(user.getUsername());
        account.setCreateTime(user.getCreateTime());
        account.setAccountId(user.getId());
        account.setBalance(0d);
        accountMapper.insert(account);

        user.setAccountId(account.getAccountId());
        loginUserMapper.updateById(user);

        return user.getId();
    }

    /**
     * 通过id查询用户
     * @param id
     * @return
     */
    @Override
    public User findById(Long id) {

        User user = loginUserMapper.selectById(id);

        return user;
    }

    /**
     * 通过电话号码查询
     * @param phone
     * @return
     */
    @Override
    public User findByPhone(String phone) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        User user = loginUserMapper.selectOne(queryWrapper);
        return user;
    }
}
